<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>日志列表</title>
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/layui/layuiAdmin/css/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-radius: 0;border-top: 4px solid #d2d2d2;">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">搜索条件</label>
          <div class="layui-input-inline">
            <select name="searchType">
              <option value="macAddress">MAC地址</option>
              <option value="eventId">事件ID</option>
<!--              <option value="timestamp">时间</option>-->
              <!-- 可以根据需要添加更多搜索条件 -->
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">关键词</label>
          <div class="layui-input-inline">
            <input type="text" name="keywords" placeholder="请输入查询内容" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-hostadmin" lay-submit lay-filter="LAY-syshost-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    <div class="layui-card-body">

      <div style="padding-bottom: 10px;">
<!--        <button class="layui-btn layuiadmin-btn-hostadmin" data-type="batchdel">删除</button>-->
        <!--<button class="layui-btn layuiadmin-btn-hostadmin" data-type="add">添加</button> -->
        <button class="layui-btn layuiadmin-btn-hostadmin" data-type="refresh">刷新</button>
      </div>

      <!--  表格数据加载 -->
      <table id="LAY-host-manage" lay-filter="LAY-host-manage"></table>
      <!--  创建时间格式化处理 -->
      <script type="text/html" id="createTimeTpl">
        {{formatDate(d.createTime)}}
      </script>

      <!--  更新时间格式化处理 -->
      <script type="text/html" id="updateTimeTpl">
        {{formatDate(d.updateTime)}}
      </script>

    </div>
  </div>
</div>

<script src="/layui/layui.js"></script>
<script type="text/javascript">
  // 日期格式转换函数
  function formatDate(str) {
    if(str != null){
      var now = new Date(str);
      var year = now.getFullYear();  //取得4位数的年份
      var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
      var date = now.getDate();      //返回日期月份中的天数（1到31）
      var hours = now.getHours();
      var minutes = now.getMinutes();
      var seconds = now.getSeconds();
      return year + "-" + month + "-" + date +" "+hours+":"+minutes+":"+seconds;
    }
    return "";
  }
</script>
<script type="text/javascript">
  layui.config({
    base: '/layui/layuiAdmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['layer','table','form'], function(){
    var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table;

    //加载table
    table.render({
      elem: '#LAY-host-manage'
      ,method: 'post'
      ,url:'/log/list' //后端接口
      ,headers: {
        'Authorization': localStorage.getItem('token')
      }
      ,width: 'auto'// 自适应宽度
      ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
      ,title: '日志数据表'
      ,cols: [
        [
          // {type: 'checkbox', fixed: 'left'}
          // ,{field:'id',title:'ID',width: 40}
          ,{field:'macAddress', title:'MAC地址',width: 160}
          ,{field:'eventId', title:'事件ID',width: 80,}
          ,{field:'timestamp', title:'时间',width: 250,}
          ,{field:'data', title:'事件详情',width: 240,}
          ,{field:'riskLevel', title:'风险判定',width: 80,templet:function (d){
            if(d.riskLevel === '低'){
              return "<button class='layui-btn layui-btn-normal layui-btn-xs'>低</button>";
            }else if(d.riskLevel === '高'){
              return "<button class='layui-btn layui-btn-danger layui-btn-xs'>高</button>";}
            else if(d.riskLevel === '中'){
              return "<button class='layui-btn layui-btn-warm layui-btn-xs'>中</button>";
            }
          }},
          {field: 'aiResult',title: 'AI分析',width: 600,templet: function(d){
              return d.aiResult;}
          }

        ]]
      ,page: {
        curr: 1 //设定初始在第 5 页
        ,limit: 5 //设定初始每页显示 5 条
      },
      limits:[5,10,15,20]
    });


    //监听搜索
    form.on('submit(LAY-syshost-front-search)', function(data){
      var field = data.field;
      //执行重载
      table.reload('LAY-host-manage', {
        url: '/log/list',
        method: "post",
        headers: {
          'Authorization': localStorage.getItem('token')
        },
        where:{
          searchField: field.searchType,
          keywords: field.keywords
        }
      });
    });

    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-host-manage')
                ,checkData = checkStatus.data; //得到选中的数据

        if(checkData.length == 0){
          return layer.msg('请选择数据');
        }

        // 程序走到这里，表示已经有选中要删除的数据了。
        layer.confirm('确定删除吗？',function (){
          var hostIds = new Array();
          $.each(checkData,function (index,item){
            hostIds.push(item.id);
          })
          // 提交给后端
          $.ajax({
            url:"/host/delete",
            type:"post",
            headers: {
              'Authorization': localStorage.getItem('token')
            },
            data:{ids:hostIds},
            dataType:"json",
            success:function (res){
              if(res.code == 0){//成功
                layer.msg(res.msg);
                table.reload('LAY-host-manage'); //数据刷新
                layer.close(index); //关闭弹层
              }else{
                layer.msg(res.msg,{icon: 5});//弹出提示
              }
            }
          })
        })
      }
      ,add: function(){
        layer.open({
          type: 2
          ,title: '添加用户'
          ,skin: 'layui-layer-molv'
          ,content: '/page/host/add'
          ,maxmin: true
          ,area: ['580px', '350px']
          ,btn: ['确定', '取消']
          ,yes: function (index,layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submitID = 'LAY-host-front-submit'
                    ,submit = layero.find('iframe').contents().find('#'+ submitID);
            //监听提交--隐藏按钮
            iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
              var field = data.field; //获取提交的字段
              // 通过Ajax方式将参数提交到后端--保存数据库中
              $.ajax({
                url:"/host/save",
                type:"post",
                headers: {
                  'Authorization': localStorage.getItem('token')
                },
                data:JSON.stringify(field),
                contentType: "application/json;charset=utf-8", //提交参数类型为json格式
                dataType:"json",
                success:function (res){
                  if(res.code === 0){//成功
                    layer.msg(res.msg);//弹出提示
                    table.reload('LAY-host-manage'); //数据刷新
                    layer.close(index); //关闭弹层
                  }else{//系统出问题
                    layer.msg(res.msg,{icon: 5});//弹出提示
                  }
                }
              })
            });
            submit.trigger('click');
          }

        });
      }
      ,refresh: function(){
        table.reload('LAY-host-manage', {
          url: '/log/list',
          method: "post",
          headers: {
            'Authorization': localStorage.getItem('token')
          }
        });
      }
    };

    $('.layui-btn.layuiadmin-btn-hostadmin').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

  });
</script>
</body>
</html>